<#include "public.ftl">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.dao.${baseClassName}${props.classNameSuffix}Dao">

    <!-- 二级缓存配置 -->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

    <!-- ${tab.remarks} ${tab.tableName} -->
    <resultMap id="BaseResultMap" type="${basePackage}.entities.${baseClassName}">
        <#list colList as col>
        <result column="${col.columnName}" jdbcType="${col.jdbcType}" property="${col.entityProName}"/>
        </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list pkList as pk>T1.${pk.columnName}<#if pk_has_next>||'_'||</#if></#list> AS TENANT_ID,'${tab.tableName}' AS TENANT_NAME,
        <#list colList as col>
        <#if col.javaType == 'Date'>TO_CHAR(T1.${col.columnName},'YYYY-MM-DD HH24:MI:SS') AS ${col.columnName}<#else>T1.${col.columnName}</#if><#if col_has_next>,</#if>
        </#list>
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_SQL">
        <#list qoList as qo>
        <#if (qo.javaType == 'Date' &&  qo.entityProName?ends_with('BgTm'))>
        <if test="${qo.entityProName} != null">
            AND T1.${qo.columnName} >= ${r'#{'}${qo.entityProName},jdbcType=${qo.jdbcType}}
        </if>
        <#elseif (qo.javaType == 'Date' &&  qo.entityProName?ends_with('EdTm'))>
        <if test="${qo.entityProName} != null">
            AND T1.${qo.columnName} &lt;= ${r'#{'}${qo.entityProName},jdbcType=${qo.jdbcType}}
        </if>
        <#elseif ((qo.jdbcType == 'Number' || qo.jdbcType == 'Numeric') &&  qo.entityProName?ends_with('Min'))>
        <if test="${qo.entityProName} != null">
            AND T1.${qo.columnName} >= ${r'#{'}${qo.entityProName},jdbcType=${qo.jdbcType}}
        </if>
        <#elseif ((qo.jdbcType == 'Number' || qo.jdbcType == 'Numeric') &&  qo.entityProName?ends_with('Max'))>
        <if test="${qo.entityProName} != null">
            AND T1.${qo.columnName} &lt;= ${r'#{'}${qo.entityProName},jdbcType=${qo.jdbcType}}
        </if>
        <#elseif qo.entityProName?ends_with('List')>
        <if test="${qo.entityProName} != null and ${qo.entityProName}.size() > 0">
            <choose>
                <when test="${qo.entityProName}.size() == 1">
                    AND T1.${qo.columnName} = ${r'#{'}${qo.entityProName}[0]}
                </when>
                <otherwise>
                    AND T1.${qo.columnName} IN
                    <foreach collection="${qo.entityProName}" open="(" close=")" separator="," item="_item">
                        ${r'#{'}_item}
                    </foreach>
                </otherwise>
            </choose>
        </if>
        <#elseif qo.javaType == 'String'>
        <if test="${qo.entityProName} != null and ${qo.entityProName} != ''">
        AND T1.${qo.columnName} LIKE ${r'#{'}${qo.entityProName},jdbcType=${qo.jdbcType}}||'%'
        </if>
        <#else>
        </#if>
    </#list>
    </sql>
    
    <!-- 根据主键查询 -->
    <select id="selectByPrimaryKey" parameterType="<@serviceMethodPKFullType pkList=pkList baseClassName=baseClassName basePackage=basePackage></@serviceMethodPKFullType>"
            resultType="java.util.HashMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tab.tableName} T1
        WHERE
        <#list pkList as pk>
            T1.${pk.columnName} = ${r'#{'}${pk.entityProName},jdbcType=${pk.jdbcType}}<#if pk_has_next>,</#if>
        </#list>
    </select>
    
    <!-- 根据条件查询 -->
    <select id="selectList" parameterType="${basePackage}.entities.${baseClassName}Qo"
            resultType="java.util.HashMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tab.tableName} T1
        <where>
            <include refid="Base_Where_SQL"/>
        </where>
    </select>

    <#if props.partition??>
    <!-- 查询最新监测数据 -->
    <select id="selectLatest" parameterType="${basePackage}.entities.${baseClassName}Qo"
            resultType="java.util.HashMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM (
        SELECT T1.*,${props.partition.by}
        FROM ${tab.tableName} T1
        <where>
            <include refid="Base_Where_SQL"/>
        </where>
        )
        WHERE RN = 1
    </select>
    </#if>


    <!-- 新增 -->
    <insert id="insert" parameterType="${basePackage}.entities.${baseClassName}">
        INSERT INTO ${tab.tableName} (
        <#list colList as col>
            ${col.columnName}<#if col_has_next>,</#if>
        </#list>
        )
        VALUES (
        <#list colList as col>
            ${r'#{'}${col.entityProName},jdbcType=${col.jdbcType}}<#if col_has_next>,</#if>
        </#list>
        )
    </insert>

    <!-- 新增(null字段会被忽略) -->
    <insert id="insertSelective" parameterType="${basePackage}.entities.${baseClassName}">
        INSERT INTO ${tab.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list colList as col>
            <if test="${col.entityProName} != null">
                ${col.columnName}<#if col_has_next>,</#if>
            </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list colList as col>
            <if test="${col.entityProName} != null">
                ${r'#{'}${col.entityProName},jdbcType=${col.jdbcType}}<#if col_has_next>,</#if>
            </if>
            </#list>
        </trim>
    </insert>
    
    <!-- 根据主键更新(null字段会被忽略) -->
    <update id="updateByPrimaryKeySelective" parameterType="${basePackage}.entities.${baseClassName}">
        UPDATE ${tab.tableName}
        <set>
            <#list colList as col>
            <if test="${col.entityProName} != null">
                ${col.columnName} =  ${r'#{'}${col.entityProName},jdbcType=${col.jdbcType}}<#if col_has_next>,</#if>
            </if>
            </#list>
        </set>
        WHERE
        <#list pkList as pk>
            ${pk.columnName} = ${r'#{'}${pk.entityProName},jdbcType=${pk.jdbcType}}<#if pk_has_next>,</#if>
        </#list>
    </update>
    
    <!-- 根据主键更新 -->
    <update id="updateByPrimaryKey" parameterType="${basePackage}.entities.${baseClassName}">
        UPDATE ${tab.tableName}
        SET
        <#list colList as col>
        ${col.columnName} =  ${r'#{'}${col.entityProName},jdbcType=${col.jdbcType}}<#if col_has_next>,</#if>
        </#list>
        WHERE
        <#list pkList as pk>
            ${pk.columnName} = ${r'#{'}${pk.entityProName},jdbcType=${pk.jdbcType}}<#if pk_has_next>,</#if>
        </#list>
    </update>

    <!-- 根据主键删除 -->
    <delete id="deleteByPrimaryKey" parameterType="<@serviceMethodPKFullType pkList=pkList baseClassName=baseClassName basePackage=basePackage></@serviceMethodPKFullType>">
        DELETE FROM ${tab.tableName}
        WHERE
        <#list pkList as pk>
            ${pk.columnName} = ${r'#{'}${pk.entityProName},jdbcType=${pk.jdbcType}}<#if pk_has_next>,</#if>
        </#list>
    </delete>

    <!-- 根据条件删除 -->
    <delete id="deleteByFilter" parameterType="${basePackage}.entities.${baseClassName}Qo">
        DELETE FROM ${tab.tableName} T1
        <where>
            <include refid="Base_Where_SQL"/>
        </where>
    </delete>
</mapper>